var ActivityTable = {
    id: "activityTable",//表格id
    seItem: null,		//选中的条目
    me : ""
};

/**
 * 初始化整个表格
 */
ActivityTable.init = function(){
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(ActivityTable.id,Feng.ctxPath+"/activity/allParam",defaultColunms);
    bstable.setData({});
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
ActivityTable.initColumn = function(){
    var columns = [
        {title: '序号',align: 'center',valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index+1;
            }},
        {title: '活动名称',field: 'activityName',align: 'center',valign: 'middle'},
        {title: '活动类型',field: 'activityType',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                return "优惠活动";
            }},
        {title: '折扣类型',field: 'discountType',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                if (value == 1){
                    return  "折扣";
                }else if(value ==2){
                    return  "一口价";
                }
            }},
        {title: '折扣程度',field: 'discount',align: 'center',valign: 'middle'},
        {title: '活动开始时间',field: 'activityTimeStart',align: 'center',valign: 'middle'},
        {title: '活动结束时间',field: 'activityTimeEnd',align: 'center',valign: 'middle'},
        {title: '活动简介',field: 'instruction',align: 'center',valign: 'middle',cellStyle:formatTableUnit,
            formatter:function(value,row,index){
                if(row.activityTimeStart > 5){
                    return "<span title='"+value+"'>"+value.substring(0,6)+"..."+"</span>";
                }else{
                    return "<span title='"+value+"'>"+value.substring(0,value.length)+"</span>";
                }
            }},
        {title: '当前状态',field: 'instruction',align: 'center',valign: 'middle',cellStyle:formatTableUnit,
            formatter:function(value,row,index){
                var now = new Date();
                var start = new Date(row.activityTimeStart);
                var end = new Date(row.activityTimeEnd);

                if(now < start){
                    return "<span class='label label-default'>未开始</span>";
                }else if(now >= start && now <= end){
                    return "<span class='label label-success'>进行中</span>";
                }else if(now > end){
                    return "<span class='label label-warning'>已结束</span>";
                }
            }},
        {title: '操作',field: '#',align: 'center',
            formatter:function(value,row,index){
                var e = '<button class="btn btn-primary btn-xs" onclick="editBtn(\''+ row.id + '\')" title="编辑"><i class="glyphicon glyphicon-pencil"></i></button> &nbsp;&nbsp;';
                var f = '<button class="btn btn-danger btn-xs" onclick="deleteBtn(\''+ row.id+'\',\''+ row.activityName+'\')" title="删除"><i class="glyphicon glyphicon-trash"></i></button>';
                return e+f;
            }
        }];
    return columns;
};

/**
 * 表格列样式
 */
function formatTableUnit() {
    return {
        css: {
            "overflow": 'hidden',
            "text-overflow":'ellipsis',
            "white-space":'nowrap'
        }
    };
}

/**
 * 点击搜索按钮
 * @returns
 */
function clickSearch(){
    var queryData = {};
    var val_search_activityname= $("#txt_search_activityname").val();

    if (val_search_activityname != "" || val_search_activityname != null){
        queryData['activityName'] =  val_search_activityname;
    }
    ActivityTable.me.destroy(queryData);
    // ActivityTable.me.refresh({query: queryData});
}

/**
 * 点击添加按钮时，跳出添加活动对话框
 * @returns
 */
function clickAddBtn(){
    ActivityInfoDlg.clearData();
    var index = layer.open({
        type: 1,
        title: '添加活动',
        skin: 'layui-layer-lan', //加上边框
        area: [ActivityInfoDlg.width, 'auto'],//宽高
        content: ActivityInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            ActivityInfoDlg.renderAdd();

        },
        maxmin:true,
        scrollbar: false,
        btn:['保存','关闭'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            ActivityInfoDlg.addSubmit(index);
            //layer.close(index); //如果设定了yes回调，需进行手工关闭
        },btn2:function (index) {
            layer.close(index);
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    layer.full(index);
    ActivityInfoDlg.currentLayerIndex = index;
}

/**
 * 点击编辑按钮时，跳出活动详情对话框
 * @returns
 */
function editBtn(uid){
    ActivityInfoDlg.clearData();
    var index = layer.open({
        type: 1,
        title: '编辑活动',
        skin: 'layui-layer-lan',
        area: [ActivityInfoDlg.width,ActivityInfoDlg.height],
        content: ActivityInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            ActivityInfoDlg.renderAdd();
            ActivityInfoDlg.activityId = uid;
            ActivityInfoDlg.load(uid);
        },
        maxmin:true,
        scrollbar: false,
        btn:['保存','关闭'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            ActivityInfoDlg.editSubmit();
            layer.close(index); //如果设定了yes回调，需进行手工关闭
            ActivityInfoDlg.activityId = "";
        },btn2:function (index) {
            layer.close(index);
            ActivityInfoDlg.activityId = "";
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
            ActivityInfoDlg.activityId = "";
        }
    });
    layer.full(index);
    ActivityInfoDlg.currentLayerIndex = index;
}


/**
 * 删除某个活动
 * @param uid 活动的id
 * @param deleteName  活动名称
 * @returns
 */
function deleteBtn(uid,deleteName){
    layer.confirm('是否删除活动:'+deleteName, {icon: 10, title:'是否删除活动?'}, function(index){
        var ajax = new $ax(Feng.ctxPath + "/activity/deleteById", function(data){
            if (data.success){
                Feng.success("删除成功!");
                ActivityTable.me.refresh();
                layer.close(index);
            }else if(data.code === 2){
                Feng.error(data.msg);
            }else{
                Feng.error("删除失败!");
            }
        },function(data){
            Feng.error("删除失败!");
        });
        ajax.data={id:uid};
        ajax.start();
    });
}

$(function(){
    //初始化活动管理表格
    ActivityTable.init();

    ActivityInfoDlg.init();

    ActivityBatchInfoDlg.init();

    // console.log($("#activityDialog").html());
    // // alert($("#discountType1").val());
    // $(":radio[name='discount']").click(function () {

    //
    // });

});
